package com.longjianghuasheng.logistics.business.advisory

import android.os.Bundle
import android.widget.CompoundButton
import androidx.fragment.app.setFragmentResultListener
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.inquiry.InquiryFragment
import com.longjianghuasheng.logistics.databinding.FragmentAskForAQuotationBinding
import com.longjianghuasheng.logistics.entity.AddressResponse
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.toastNormal
import com.longjianghuasheng.logistics.util.CacheUtil

class AskForAQuotationFragment :
    BaseTitleFragment<AskForAQuotationViewModel, FragmentAskForAQuotationBinding>() {
    override fun layoutId(): Int = R.layout.fragment_ask_for_a_quotation

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("咨询报价", CacheUtil.isDriver)
        mDatabind.initListener()

    }

    fun FragmentAskForAQuotationBinding.initListener() {


        stvStart.setRightImageViewClickListener {

            navigate(R.id.addressListFragment, Bundle().apply {
                putString("type", InquiryFragment.KEY_START_ADDRESS)
            })
        }

        stvEnd.setRightImageViewClickListener {

            navigate(R.id.addressListFragment, Bundle().apply {
                putString("type", InquiryFragment.KEY_END_ADDRESS)
            })
        }


        rgClaimGoods.setOnCheckedChangeListener { group, checkedId ->
            mViewModel.get_type = if (checkedId == R.id.rb_container) 1 else 2

        }
        rbLandTransportation.setOnCheckedChangeListener(::onCheckListener)
        cbSeaTransportation.setOnCheckedChangeListener(::onCheckListener)
        cbRailway.setOnCheckedChangeListener(::onCheckListener)
        cbService.setOnCheckedChangeListener(::onCheckListener)
        rgTheGoodsToThePlace.setOnCheckedChangeListener { group, checkedId ->
            mViewModel.place_type = if (checkedId == R.id.rb_fixed_point) 1 else 2
        }
        tvProtocol.setOnClickListener {

            navigate(R.id.aboutFragment, Bundle().apply {
                putInt("id", 1)
            })
        }

        btnCommit.setOnClickListener {

            if (!cbConfirm.isChecked) {
                toastNormal("请勾选下面的协议")
                return@setOnClickListener
            }

            mViewModel.run {
                sent_type = arrayMap.values.filterNot { it == 0 }.toMutableList()

                names = stvNames.centerEditValue
                start_address = stvStart.centerEditValue
                end_address = stvEnd.centerEditValue
                types = stvTypes.centerEditValue
                weights = stvWeight.centerEditValue
                volume = stvVolume.centerEditValue

                service_form()
            }
        }
    }

    private fun onCheckListener(buttonView: CompoundButton, isChecked: Boolean) {

        mViewModel.arrayMap.run {

            when (buttonView.id) {
                R.id.rb_land_transportation -> put(buttonView.id, if (isChecked) 1 else 0)
                R.id.cb_sea_transportation -> put(buttonView.id, if (isChecked) 2 else 0)
                R.id.cb_railway -> put(buttonView.id, if (isChecked) 3 else 0)
                R.id.cb_service -> put(buttonView.id, if (isChecked) 4 else 0)
                else -> {}
            }
        }


    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {

            mDatabind.clear()

            if (!CacheUtil.isDriver) {
                navigateUp()
            }
        }
        setFragmentResultListener(InquiryFragment.KEY_START_ADDRESS) { requestKey, bundle ->
            bundle.getParcelable<AddressResponse>("address")?.let {

                val address = it.province + it.city + it.area + it.address
                mViewModel.start_address = address
                mDatabind.stvStart.setCenterEditString(address)

            }


        }
        setFragmentResultListener(InquiryFragment.KEY_END_ADDRESS) { requestKey, bundle ->
            bundle.getParcelable<AddressResponse>("address")?.let {
                val address = it.province + it.city + it.area + it.address
                mViewModel.end_address = address
                mDatabind.stvEnd.setCenterEditString(address)

            }


        }
    }

    private fun FragmentAskForAQuotationBinding.clear() {
        stvStart.setCenterEditString("")
        stvEnd.setCenterEditString("")
        stvNames.setCenterEditString("")
        stvTypes.setCenterEditString("")
        stvWeight.setCenterEditString("")
        stvVolume.setCenterEditString("")

        rgClaimGoods.check(R.id.rb_container)
        rbLandTransportation.isChecked = true
        cbSeaTransportation.isChecked = false
        cbRailway.isChecked = false
        cbService.isChecked = false


        rgTheGoodsToThePlace.check(R.id.rb_fixed_point)

    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

}